package com.jyy.prefabricated.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.BatchDto;
import com.jyy.prefabricated.dto.Pageable;
import com.jyy.prefabricated.entity.StudentService;
import com.jyy.prefabricated.exception.CustomException;
import com.jyy.prefabricated.service.StudentServiceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 学员服务表(StudentService)表控制层
 *
 * @author makejava
 * @since 2021-05-13 14:42:58
 */
@Slf4j
@RestController
@RequestMapping("/api")
public class StudentServiceController {
    /**
     * 服务对象
     */
    @Resource
    private StudentServiceService studentServiceService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public StudentService selectOne(Integer id) {
        return this.studentServiceService.queryById(id);
    }


    /**
     * 新增学员服务
     *
     * @param studentService
     * @return
     */
    @PostMapping("/student-service")
    public AjaxResult insert(@Valid @RequestBody StudentService studentService) {
        StudentService service = studentServiceService.insert(studentService);
        StudentService result = studentServiceService.queryById(service.getId());
        return AjaxResult.success(result);
    }

    /**
     * 更新学员服务
     *
     * @param studentService
     * @return
     */
    @PutMapping("/student-service")
    public AjaxResult update(@Valid @RequestBody StudentService studentService) {

        studentServiceService.update(studentService);
        StudentService result = studentServiceService.queryById(studentService.getId());
        return AjaxResult.success(result);
    }

    /**
     * 发布/取消发布学员服务
     *
     * @param dto
     * @return
     */
    @PutMapping("/student-service-batch")
    public AjaxResult update(@Valid @RequestBody BatchDto dto) {
        if (dto.getIds().size() == 0) {
            throw new CustomException("参数id不能为空");
        }
        studentServiceService.batchUpdateStatus(dto);
        return AjaxResult.success();
    }

    /**
     * 删除学员服务
     *
     * @param dto
     * @return
     */
    @DeleteMapping("/student-service-batch")
    public AjaxResult delete(@Valid @RequestBody BatchDto dto) {
        if (dto.getIds().size() == 0) {
            throw new CustomException("参数id不能为空");
        }
        studentServiceService.batchUpdateDeleted(dto);
        return AjaxResult.success();
    }


    /**
     * 查询学员服务详情
     *
     * @param id
     * @return
     */
    @GetMapping("/student-service/{id}")
    public AjaxResult selectById(@PathVariable Integer id) {
        log.info("Rest request to find course by id:{}", id);
        StudentService result = studentServiceService.queryById(id);
        return AjaxResult.success(result);
    }

    /**
     * 后台分页查询学员服务列表
     *
     * @param serviceName 名称
     * @param status      发布状态
     * @param pageable    分页信息
     * @return
     */
    @GetMapping("/student-service")
    public AjaxResult selectByParams(@RequestParam(required = false, defaultValue = "") String serviceName, @RequestParam(required = false) Integer status, Pageable pageable) {

        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<StudentService> studentServiceList = studentServiceService.queryList(serviceName, status);
        PageInfo<StudentService> result = new PageInfo<>(studentServiceList);
        return AjaxResult.success(result);
    }

    /**
     * 后台查询学员服务列表
     *
     * @param serviceName 名称
     * @param status      发布状态
     * @return
     */
    @GetMapping("/student-service-list")
    public AjaxResult selectByParams(@RequestParam(required = false, defaultValue = "") String serviceName, @RequestParam(required = false, defaultValue = "0") Integer status) {

        List<StudentService> result = studentServiceService.queryList(serviceName, status);
        return AjaxResult.success(result);
    }

}
